package pms.sg.service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import pms.common.value.sgListValue;
import org.springframework.jdbc.support.rowset.SqlRowSet;
import pms.common.dao.ListSGDao;
import pms.common.dao.DetailSGDao;

public class DetailSGServiceImpl implements DetailSGService{
	private int currentStatus = -1;
	private ListSGDao listSGDao;
	private DetailSGDao detailSGDao;
	public DetailSGDao getDetailSGDao() {
		return detailSGDao;
	}
	public void setDetailSGDao(DetailSGDao detailSGDao) {
		this.detailSGDao = detailSGDao;
	}
	public ListSGDao getListSGDao() {
		return listSGDao;
	}
	public void setListSGDao(ListSGDao listSGDao) {
		this.listSGDao = listSGDao;
	}
	public int getCurrentStatus() {
		return currentStatus;
	}
	public void setCurrentStatus(int currentStatus) {
		this.currentStatus = currentStatus;
	}
	public String dateFormat(String datestr) {
		String[] strtemp = new String[3];
		String getDateStr = "";
		
		if(datestr.length()==8){
			strtemp[0] = datestr.substring(0,4);
			strtemp[1] = datestr.substring(4,6);
			strtemp[2] = datestr.substring(6,8);
		getDateStr = strtemp[0]+"/"+strtemp[1]+"/"+strtemp[2];
		}else{
			getDateStr = "    /  /  ";
		}
		return getDateStr;
	}
	
	public int getStatus(String currStatus) {
		if(currStatus.matches("未対応")){
			return 0;
		}
		else if(currStatus.matches("分析中")){
			return 1;
		}else if(currStatus.matches("対応済み")){
			return 2;
		}else{
			return -1;
		}
	}
	@SuppressWarnings("unchecked")
	public List getMonthArray() {
		List l_list = new ArrayList();
		for (int i=0;i<12;i++) {
			if (i<9) {
				l_list.add(i,"0" + (i+1));
			} else {
				l_list.add(i,"" + (i+1));
			}
		}
		return l_list;
	}
	
	@SuppressWarnings("unchecked")
	public List getDayArray() {
		List l_list = new ArrayList();
		for (int i=0;i<31;i++) {
			if (i<9) {
				l_list.add(i,"0" + (i+1));
			} else {
				l_list.add(i,"" + (i+1));
			}
		}
		return l_list;
	}
	// ステータス
	@SuppressWarnings("unchecked")
	public List<HashMap> getStatusArray() throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException {   
		List<HashMap> l_array = new ArrayList();
        SqlRowSet l_rs = listSGDao.getMasterData("bug_status");
        while(l_rs.next()) {
        	HashMap l_hashMap = new HashMap();
          	l_hashMap.put("master_id",l_rs.getString("MASTER_ID").trim());
          	l_hashMap.put("master_group",l_rs.getString("MASTER_GROUP").trim());
          	l_hashMap.put("master_value",l_rs.getString("MASTER_VALUE").trim());
        	l_array.add(l_hashMap);
        }
        System.out.println("ZH_STATUS:" + l_array);
        if(currentStatus==0){
			l_array.remove(2);
		}else if(currentStatus==1){
			l_array.remove(0);
		}else if(currentStatus==2){
			l_array = l_array.subList(2,3);
		}
        return l_array;
    }
	
	// 修正担当側
	@SuppressWarnings("unchecked")
	public List<HashMap> getModifypartArray() throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException {   
		List<HashMap> l_array = new ArrayList();
        SqlRowSet l_rs = listSGDao.getMasterData("test_part");
        while(l_rs.next()) {
        	HashMap l_hashMap = new HashMap();
          	l_hashMap.put("master_id",l_rs.getString("MASTER_ID").trim());
          	l_hashMap.put("master_group",l_rs.getString("MASTER_GROUP").trim());
          	l_hashMap.put("master_value",l_rs.getString("MASTER_VALUE").trim());
        	l_array.add(l_hashMap);
        }
        System.out.println("EDITER:" + l_array);
        return l_array;
    }
	
	// 修正担当
	@SuppressWarnings("unchecked")
	public List<HashMap> getUserArray() throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException {
		List<HashMap> l_array = new ArrayList();
		l_array = listSGDao.getUserData();
		return l_array;
	}
	
	// 障害要因
	@SuppressWarnings("unchecked")
	public List<HashMap> getReasonArray() throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException {   
		List<HashMap> l_array = new ArrayList();
        SqlRowSet l_rs = listSGDao.getMasterData("bug_reason");
        while(l_rs.next()) {
        	HashMap l_hashMap = new HashMap();
          	l_hashMap.put("master_id",l_rs.getString("MASTER_ID").trim());
          	l_hashMap.put("master_group",l_rs.getString("MASTER_GROUP").trim());
          	l_hashMap.put("master_value",l_rs.getString("MASTER_VALUE").trim());
        	l_array.add(l_hashMap);
        }
        System.out.println("ZH_REASON:" + l_array);
        return l_array;
    }
	public sgListValue setzhListData(String no) throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException {
		sgListValue zhList = new sgListValue();
		SqlRowSet l_zhListData = detailSGDao.getListdetailData(no);
		HashMap proList = listSGDao.getProject();
		HashMap themeList = listSGDao.getTheme();
		if(l_zhListData.next()){
			zhList.setNo(no.trim());
			if(proList.containsKey(String.valueOf(l_zhListData.getString("PROJECTID").trim()))){
				zhList.setProject(String.valueOf(proList.get(l_zhListData.getString("PROJECTID").trim())));
    		} else {
    			zhList.setProject(l_zhListData.getString("PROJECTID").trim());
    		}
    		if(themeList.containsKey(String.valueOf(l_zhListData.getString("TEMA").trim()))){
    			zhList.setTema(String.valueOf(themeList.get(l_zhListData.getString("TEMA").trim())));
    		} else {
    			zhList.setTema(l_zhListData.getString("TEMA").trim());
    		}
			zhList.setTest_lvl(l_zhListData.getString("TEST_LVL"));
			zhList.setTest_part(l_zhListData.getString("TEST_PART"));
			zhList.setTester(l_zhListData.getString("TESTER"));
			zhList.setTest_date(dateFormat(l_zhListData.getString("TEST_DATE")));
			zhList.setStatus(l_zhListData.getString("ZH_STATUS"));
			zhList.setMemo(l_zhListData.getString("MEMO"));
			zhList.setError_text(l_zhListData.getString("ERROR_TEXT"));
			zhList.setLimit_date(dateFormat(l_zhListData.getString("LIMIT_DATE")));
			zhList.setQa_memo(l_zhListData.getString("QA_MEMO"));
			zhList.setModify_part(l_zhListData.getString("MODIFY_PART"));
			zhList.setModify(l_zhListData.getString("ZH_MODIFY"));
			zhList.setMehood(l_zhListData.getString("MEHOOD"));
			zhList.setReason(l_zhListData.getString("REASON"));
			zhList.setReasons(l_zhListData.getString("REASONS"));
			zhList.setEdit_date(l_zhListData.getString("EDIT_DATE"));
			currentStatus=getStatus(l_zhListData.getString("ZH_STATUS").trim());
		}
		return zhList;
	}
	public void updateListdetailData(sgListValue zhList) throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException{
		String modifyDate=zhList.getModify_Date_Y()+zhList.getModify_Date_M()+zhList.getModify_Date_D();
       System.out.println("modifyDate:::::::::::"+zhList.getModify_Date_Y());
       System.out.println("modifyDate:::::::::::"+modifyDate);
       System.out.println("Memo"+zhList.getMemo());
       detailSGDao.updateListdetailData(zhList.getStatus(),zhList.getModify_part(),zhList.getModify(),zhList.getMehood(),zhList.getReason(),zhList.getReasons(),modifyDate,zhList.getNo());
	}
}
